import requests
import json
def base64_image(img_path):
    '''\
    img_path :图片连接
    return :  返回图片的base64编译字符串
    '''
    import base64
    with open(img_path,'rb') as f:
        base64_data = base64.b64encode(f.read())

    return str(base64_data,'utf-8')


class ReturnAiFaceUrl:
    detect_url_cla = 'https://aip.baidubce.com/rest/2.0/face/v3/detect?\
access_token={}'#提交检测图片的链接
    match_url_cla = 'https://aip.baidubce.com/rest/2.0/face/v3/match?\
access_token={}'#提交对比图片的链接
    host_cla = 'https://aip.baidubce.com/oauth/2.0/\
token?grant_type=client_credentials&client_id=\
{}&client_secret={}'#获取access_token值的链接
    header = {'Content-Type': 'application/json; charset=UTF-8'} 
    
    def __init__(self,
                 ak = 'ARAhQ54l9eTGmUhszx8iPZAF',
                 sk = 'aG5urwxj3U1FWidG1BIFWp79oyNVEky7',
                 ):
        
        self.ak = ak
        
        self.sk = sk
        
        self.host = self.host_cla.format(ak,sk)
        ##获取access_token值的链接
        
        self.access_token = requests.get(
            self.host,
            headers = self.header).json()['access_token']
        
        self.detect_url = self.detect_url_cla.format(
            self.access_token)
        
        self.match_url = self.match_url_cla.format(
            self.access_token)


        
    
if __name__ == '__main__':
    
    x = ReturnAiFaceUrl()
    print(x.access_token)

        
        




    
'''
ak = 'ARAhQ54l9eTGmUhszx8iPZAF'
sk = 'aG5urwxj3U1FWidG1BIFWp79oyNVEky7'
host = 'https://aip.baidubce.com/oauth/2.0/\
token?grant_type=client_credentials&client_id=\
{}&client_secret={}'.format(ak,sk)
print(host)

header = {'Content-Type': 'application/json; charset=UTF-8'}
response = requests.get(host,headers = header)
access_token = response.json()['access_token']
print(access_token)
detect_url = 'https://aip.baidubce.com/rest/2.0/face/v3/detect?\
access_token={}'.format(access_token)

image_base64 = base64_image(
    r'C:/Users/Administrator/Desktop/1.jpg')

info_dict = {"image":image_base64,
             "image_type":"BASE64",
             "face_field":"faceshape,facetype,beauty,age,glasses"}
params = json.dumps(info_dict)

response_detect = requests.post(detect_url,
                                headers = header,
                                data = params)
return_info = response_detect.json()
print(return_info)

'''


















